package cn.appsys.web.controller;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.UserService;
import cn.appsys.pojo.EmailMessage;
import cn.appsys.service.impl.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Random;

/**
 * Created by Super Cui on 2017/10/14.
 */
@Controller
@RequestMapping("/devuser")
public class DevUserController {

    @Autowired
    private UserService userService;
    @Autowired
    private EmailService emailService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public EmailService getEmailService() {
        return emailService;
    }

    public void setEmailService(EmailService emailService) {
        this.emailService = emailService;
    }

    /**
     * 处理登录请求
     * @param devUser
     * @param session
     * @return
     */
    @RequestMapping(value = "/login")
    public String doLogin(DevUser devUser, HttpSession session,HttpServletRequest request){
        if ((devUser = userService.doLogin(devUser)) != null) {
            session.setAttribute("devUserInfo",devUser);
            session.setAttribute("loginInfo", "已登录");
            devUser.setDevPassword("");
            session.setAttribute("loginUserInfo",devUser);
            return "redirect:" + request.getContextPath() + "/dev/dev_index.jsp";
        } else {
            devUser.setDevPassword("");
            session.setAttribute("reDevUserInfo",devUser);
            session.setAttribute("loginInfo", "用户名或密码错误！");
            session.removeAttribute("devUserInfo");
            return "dev/dev_login";
        }
    }

    /**
     * 处理注销请求
     * @param session
     * @param request
     * @return
     */
    @RequestMapping(value = "/unlogin")
    public String unLogin(HttpSession session, HttpServletRequest request) {
/*        session.removeAttribute("devUserInfo");
        session.setAttribute("loginInfo", "已经注销，请重新登录！");*/
        session.invalidate();
        return "redirect:" + request.getContextPath() + "/dev/dev_login.jsp";
    }

    /**
     * 处理注册请求
     * @param devUser
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/regist")
    public String regist(DevUser devUser,String checkNumber,String emailCode,HttpServletRequest request,HttpSession session){
        Boolean ans;
        String emailCode2 = (String) session.getAttribute("emailCode");
        if(!emailCode2.equals(emailCode)){
            ans = false;
            request.setAttribute("errEmailCode","注册失败，验证码错误！");
            request.setAttribute("regist","注册失败!");
        }else{
            try {
                ans = userService.addUser(devUser);
                if( !ans ){
                    request.setAttribute("regist","注册失败，用户名已经被注册！");
                }
            } catch (Exception e) {
                ans = false;
                request.setAttribute("regist","注册失败，请稍后再试！");
            }
        }
        if(ans){
            request.setAttribute("loginInfo","注册成功！请登录！");
            request.removeAttribute("registUserInfo");
            devUser.setDevPassword("");
            session.setAttribute("loginUserInfo",devUser);
            return "dev/dev_login";
        }else {
            request.setAttribute("registUserInfo",devUser);
            return "dev/dev_login";
        }
    }

    /**
     * 测试用户名是否可用
     * @param devUser
     * @return
     */
    @RequestMapping(value = "/checkRegistCode",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String checkRegistCode(DevUser devUser){
        Boolean ans = userService.checkRegistCode(devUser)==0;
        return ans.toString();
    }

    /**
     * 处理页面信息更新
     * @param devUser
     * @return
     */
    @RequestMapping(value = "/update",produces = "applicaiton/json;charset=utf-8")
    @ResponseBody
    public String updateDevUser(DevUser devUser,HttpSession session){
//        System.out.println( devUser.getDevName() + "请求update信息：" + devUser );
        Boolean ans;
        try {
            ans = userService.updateUser(devUser);
            if(ans){
                session.setAttribute("devUserInfo",devUser);
            }
        } catch (Exception e) {
            ans = false;
        }
        return ans.toString();
    }

    /**
     * 获取验证码
     * @param devUser
     * @param session
     * @return
     */
    @RequestMapping(value = "getEmailCode",produces = "applicaiton/json;charset=utf-8")
    @ResponseBody
    public String getEmailCode(DevUser devUser,HttpSession session){
        String emailCode = String.valueOf(new Random().nextInt(9000)+1000);
        EmailMessage emailMessage = new EmailMessage(devUser.getDevEmail(),devUser.getDevCode(), emailCode);
        System.out.println( "emailMessage:" + emailMessage );
        try {
            emailService.send(emailMessage);
            session.setAttribute("emailCode",emailCode);
            return "true";
        } catch (Exception e) {
            return "false";
        }
    }
}
